package cc.vv.btpartyjob.partyservice.view

import android.content.Context
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import cc.vv.btpartyjob.partyservice.R
import cc.vv.btpartyjob.partyservice.inter.ItemClickInterface
import cc.vv.btpartyjob.partyservice.module.bean.ServiceTypeBean
import cc.vv.lkbasecomponent.util.LKScreenUtil
import java.util.ArrayList

/**
 * 展示数据的自定义view
 * Created by  Chen.qi on 2018/10/24.
 */
class SearchTypeView : LinearLayout {
    /**
     * 上下文
     */
    private var mContext: Context? = null
    /**
     * 父布局
     */
    private var llSelectedAll: LinearLayout? = null

    constructor(context: Context) : this(context, null) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0) {
        mContext = context
    }

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        mContext = context
        val inflateView = View.inflate(context, R.layout.layout_search_view, null)
        llSelectedAll = inflateView.findViewById(R.id.ll_lsv_showAll)
        this.addView(inflateView)
    }


    /**
     * 填充选项数据
     *
     * @param optionList 数据集
     * @param itemSize   每行有几个选项
     */
    fun addAllSelectedFileView(optionList: ArrayList<ServiceTypeBean>?, itemSize: Int, itemClick: ItemClickInterface?) {
        if (null == optionList) {
            return
        }
        //清除
        clearAllJobOptionView()
        val length = optionList.size
        val sum = length / itemSize
        val add = if (length % itemSize == 0) 0 else 1
        val totalLinLayout = sum + add

        val viewWight = (LKScreenUtil.getScreenWidth() - LKScreenUtil.dp2px(46f)) / itemSize
        val layoutParams1 = LayoutParams(viewWight, LayoutParams.WRAP_CONTENT)

        for (j in 0 until totalLinLayout) {
            val honLinearLayout = LinearLayout(context)
            honLinearLayout.setPadding(0, LKScreenUtil.dp2px(10f), 0, 0)
            val iNumber = j * itemSize
            var count = 0
            for (i in iNumber until optionList.size) {
                count++
                if (count == itemSize + 1) {
                    break
                }
                val view = LayoutInflater.from(mContext).inflate(R.layout.layout_service_people_item, null)
                view.layoutParams = layoutParams1
                val descText = view.findViewById<TextView>(R.id.tv_lspI_content)
                val jobOptionObj = optionList[i]
                view.setOnClickListener {
                    itemClick?.clickItem(jobOptionObj.id)
                }
                descText.text = jobOptionObj?.name

                val tv = TextView(mContext)
                val layoutParamstv = LayoutParams(LKScreenUtil.dp2px(3f), ViewGroup.LayoutParams.WRAP_CONTENT)
                tv.layoutParams = layoutParamstv
                honLinearLayout.addView(tv)
                honLinearLayout.addView(view)
            }
            llSelectedAll?.addView(honLinearLayout)
        }
    }

    /**
     * 清除view
     */

    private fun clearAllJobOptionView() {
        llSelectedAll?.removeAllViews()
    }


}